package com.lanxin.xxform.tools;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Pattern;

public class JetDate {

	/**
	 * 获取uniqid码
	 * 
	 * @return
	 */
	public static String uniqid() {
		long timestamp = Calendar.getInstance().getTimeInMillis();
		return Long.toHexString(timestamp) + Integer.toHexString((int) (16 + Math.random() * (255 - 16)));
	}

	/**
	 * 获取当前时间戳（秒级）
	 * 
	 * @return
	 */
	public static long timeNow() {
		return Calendar.getInstance().getTimeInMillis() / 1000;
	}

	/**
	 * 获取当天时间戳（秒级，当日凌晨零点）
	 * @return
	 */
	public static long dayNow() {
		int year = Calendar.getInstance().get(Calendar.YEAR);
		int month = Calendar.getInstance().get(Calendar.MONTH) + 1;
		int day = Calendar.getInstance().get(Calendar.DATE);
		String dateStr = year + "-" + (month < 10 ? "0"+month : month) + "-" + (day < 10 ? "0"+day : day);
		return toTime(dateStr);
	}

	/**
	 * 格式化时间戳
	 * 
	 * @param timestamp
	 * @param format
	 * @return
	 */
	public static String date(long timestamp, String format) {
		if (format == null || format.equals("")) {
			format = "yyyy-MM-dd HH:mm:ss";
		}
		Date dates = new Date(timestamp * 1000);
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		return simpleDateFormat.format(dates);
	}

	/**
	 * 格式化当前时间
	 * 
	 * @param format
	 * @return
	 */
	public static String dateNow(String format) {
		if (format == null || format.equals("")) {
			format = "yyyy-MM-dd HH:mm:ss";
		}
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		return simpleDateFormat.format(calendar.getTime());
	}

	/**
	 * 字符串时间转时间戳
	 * @param dateStr
	 * @return
	 */
	public static long toTime(String dateStr) {
		if (dateStr == null || dateStr.isEmpty()) {
			return 0;
		}
		SimpleDateFormat simpleDateFormat;
		if (Pattern.matches("^\\d{4}-\\d{2}-\\d{2}$", dateStr)) {
			simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		} else if (Pattern.matches("^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$", dateStr)) {
			simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		} else if (Pattern.matches("^\\d{4}/\\d{1,2}/\\d{1,2}$", dateStr)) {
			simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
		} else {
			return -1;
		}
		try {
			Date date = simpleDateFormat.parse(dateStr);
			return date.getTime() / 1000;
		} catch (ParseException e) {
			return -1;
		}
	}

	/**
	 * 字符串时间转时间戳（自定义格式化）
	 * @param dateStr
	 * @param format
	 * @return
	 */
	public static long toTime(String dateStr, String format) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		try {
			Date date = simpleDateFormat.parse(dateStr);
			return date.getTime() / 1000;
		} catch (ParseException e) {
			return -1;
		}
	}

	/**
	 * 获取年度边界（当前1月1日至翌年1月1日）
	 * @return
	 */
	public static long[] getYearBorder() {
		int year = Calendar.getInstance().get(Calendar.YEAR);
		return new long[] {toTime(year + "-01-01"), toTime(year + 1 + "-01-01")};
	}

	/**
	 * 获取连续12个月边界（去年当月1日至当天下个月1日）
	 * @return
	 */
	public static long[] getContinueYearBorder() {
		int year = Calendar.getInstance().get(Calendar.YEAR);
		int month = Calendar.getInstance().get(Calendar.MONTH) + 1;
		if (month < 12) {
			month++;
			String m1 = month < 10 ? "-0"+month : "-"+month;
			String m2 = month < 10 ? "-0"+month : "-"+month;
			return new long[] {toTime((year-1)+m1+"-01"), toTime(year+m2+"-01")};
		} else {
			return new long[] {toTime(year+"-01-01"), toTime(year+1+"-01-01")};
		}
	}

	/**
	 * 获取季度边界（当前季度第一个月1日至当前季度翌月1日)
	 * @return
	 */
	public static long[] getQuarterBorder() {
		int year = Calendar.getInstance().get(Calendar.YEAR);
		int month = Calendar.getInstance().get(Calendar.MONTH) + 1;
		if (month < 4) {
			return new long[] {toTime(year+"-01-01"), toTime(year+1+"-04-01")};
		} else if (month > 3 && month < 7) {
			return new long[] {toTime(year+"-04-01"), toTime(year+1+"-07-01")};
		} else if (month > 6 && month < 10) {
			return new long[] {toTime(year+"-07-01"), toTime(year+1+"-10-01")};
		} else {
			return new long[] {toTime(year+"-10-01"), toTime(year+1+"-01-01")};
		}
	}

	/**
	 * 获取月度边界（当前月度1日至翌月1日）
	 * @return
	 */
	public static long[] getMonthBorder() {
		Calendar calendar = Calendar.getInstance();
		int year = Calendar.getInstance().get(Calendar.YEAR);
		int month = Calendar.getInstance().get(Calendar.MONTH) + 1;
		int d = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		String m = month < 10 ? "-0"+month : "-"+month;

		return new long[] {toTime(year+m+"-01"), toTime(year+m+"-"+d)+86400};
	}

	/**
	 * 获取当前是本周的第几天
	 * @return
	 */
	public static int getDayOfWeek() {
		int week = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
		if (Calendar.getInstance().getFirstDayOfWeek() == Calendar.SUNDAY)
			week--;
		return week;
	}

	/**
	 * 获取季度索引（季度-1）
	 * @param timestamp
	 * @return
	 */
	public static int getQuarterIndex(long timestamp) {
		Calendar calendar = Calendar.getInstance();
		if (timestamp > 0) {
			calendar.setTime(new Date(timestamp * 1000));
		}
		int month = calendar.get(Calendar.MONTH);
		if (month < 3) {
			return 0;
		} else if (month < 6) {
			return 1;
		} else if (month < 9) {
			return 2;
		}
		return 3;
	}

	//获取季度第一天
	public static long getQuarterStart(long timestamp) {
		Calendar calendar = Calendar.getInstance();
		if (timestamp > 0) {
			calendar.setTime(new Date(timestamp * 1000));
		}
		int month = calendar.get(Calendar.MONTH);
		if (month < 3) {
			return toTime(calendar.get(Calendar.YEAR) +  "-01-01");
		} else if (month < 6) {
			return toTime(calendar.get(Calendar.YEAR) +  "-04-01");
		} else if (month < 9) {
			return toTime(calendar.get(Calendar.YEAR) +  "-07-01");
		}
		return toTime(calendar.get(Calendar.YEAR) +  "-10-01");
	}

}